﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using devtm.Editor.TypeSelector;
using devtm.Collections;
using Microsoft.VisualStudio.Modeling;

namespace devtm.AutoMapper
{
    public class ViewNamespaceSelectEditor : UITypeEditor
    {

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            //return base.EditValue(context, provider, value);

            // Return the value if the value is not of type Int32, Double and Single.
            //if (value.GetType() != typeof(double) && value.GetType() != typeof(float) && value.GetType() != typeof(int))
            //    return value;

            // Uses the IWindowsFormsEditorService to display a 
            // drop-down UI in the Properties window.
            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            if (edSvc != null)
            {

                Store store = (context.Instance as UsingNamespace).Store;

                WindowSelectNamespace frm = new WindowSelectNamespace();
                string lastsee = "§";

                TableCollection<string, string> lst = new TableCollection<string, string>();
                foreach (string item in ContainerTypes.GetTypes(store, null).Select(c => c.Namespace))
                {
                    try
                    {
                        if (String.IsNullOrEmpty(item) || lastsee == item || item.Contains("<"))
                            continue;

                        if (!lst.Contains(item))
                            lst.Add(item);

                    }
                    catch (Exception)
                    {


                    }


                    lastsee = item;
                }


                frm.SetList(lst);
              

                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    value = frm.SelectedItem;

            }

            return value;


        }

        public override bool IsDropDownResizable
        {
            get
            {
                return false;
            }
        }

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
            //return base.GetEditStyle(context);
        }

    }
}
